package cn.tedu.antconlonysever.contract.dao.persist.repository.impl;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.util.PageInfoToPageDataConverter;
import cn.tedu.antconlonysever.contract.dao.persist.mapper.CustomerMapper;
import cn.tedu.antconlonysever.contract.dao.persist.repository.ICustomerRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Customer;
import cn.tedu.antconlonysever.contract.pojo.vo.ContactListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.CustomerListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.CustomerStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
@Service
@Slf4j
public class CustomerRepositoryImpl implements ICustomerRepository {
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public int insert(Customer customer) {
        return customerMapper.insert(customer);
    }

    @Override
    public int insertBatch(List<Customer> customerList) {
        return customerMapper.insertBatch(customerList);
    }

    @Override
    public int deleteById(Long id) {
        return customerMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {
        return customerMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Customer customer) {
        return customerMapper.updateById(customer);
    }

    @Override
    public int count() {

        return customerMapper.selectCount(null);
    }

    @Override
    public int countNo(String no) {
        QueryWrapper<Customer> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("no",no);
        return customerMapper.selectCount(queryWrapper);
    }

    @Override
    public int contactNoNotId(Long id, String no) {
        QueryWrapper<Customer> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("no",no)
                    .ne("id",id);
        return customerMapper.selectCount(queryWrapper);
    }

    @Override
    public CustomerStandardVO getStandardById(Long id) {

        return customerMapper.getStandardById(id);
    }

    @Override
    public PageData<CustomerListVO> list(Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<CustomerListVO> list =customerMapper.list();
        PageInfo<CustomerListVO> pageInfo = new PageInfo<>(list);
        PageData<CustomerListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
